package com.csst.business.dao.mysql.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.csst.business.dao.IMessageBoardDao;
import com.csst.business.dao.TestDao;
import com.csst.business.model.TCompany;
import com.csst.business.model.TMessageBoard;
import com.csst.business.model.TTrUser;

public class MessageBoardDaoImpl extends BaseDaoImpl implements IMessageBoardDao {

	@Override
	public void saveMessage(TMessageBoard messageBoard) {
		Session session=getSessionFactory().getCurrentSession();
		session.save(messageBoard);
		session.flush();
		session.clear();
	}

	@Override
	public List<TMessageBoard> getMessageList(int st,int ed) {
		List<TMessageBoard> messsagList = new ArrayList<TMessageBoard>();
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TMessageBoard t where t.fkMId='0' order by t.MDate desc";
		Query q=session.createQuery(sql);
		q.setFirstResult(st-1);
		q.setMaxResults(ed);
		List<TMessageBoard> list = q.list();
		if(list.size()>0){
			for(int i =0;i<list.size();i++){
				TMessageBoard mess = new TMessageBoard();
				mess = (TMessageBoard)list.get(i);
				messsagList.add(mess);
			}
		}
		session.flush();
		session.clear();
		return messsagList;
	}

	@Override
	public boolean haveChildDate(String id) {
		boolean flag = false;
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TMessageBoard t where t.fkMId = '"+id+"'";
		List list = session.createQuery(sql).list();
		if(list.size()>0){
			flag = true;
		}
		session.flush();
		session.clear();
		return flag;
	}

	@Override
	public boolean deleteDate(String id) {
		Session session=getSessionFactory().getCurrentSession();
		String hql=" delete from TMessageBoard t where t.id='"+id+"'";
		session.createQuery(hql).executeUpdate();
		session.flush();
		session.clear();
		return true;
	}

	@Override
	public List<TMessageBoard> getChildMessageByFkId(String msid) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TMessageBoard t where t.fkMId = '"+msid+"' order by t.MDate desc";
		List<TMessageBoard> list = session.createQuery(sql).list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public int getMessageTotle() {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from TMessageBoard t where t.fkMId='0' order by t.MDate desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}


}
